package com.dq.health.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dq.health.dao.prescription.PrescriptionOrderMapper;
import com.dq.health.dto.request.prescription.QueryPrescriptionParam;
import com.dq.health.dto.request.prescription.SavePrescriptionRequest;
import com.dq.health.dto.response.prescription.PrescriptionDetailInfo;
import com.dq.health.dto.response.prescription.PrescriptionListInfo;
import com.dq.health.entity.prescription.PrescriptionOrder;
import com.dq.health.entity.user.UserBaseInfo;
import com.dq.health.enums.GeneratorIdTypeEnum;
import com.dq.health.enums.PrescriptionStatusEnum;
import com.dq.health.exception.BizRtException;
import com.dq.health.service.IAPIGeneratorIdService;
import com.dq.health.service.IAPIPrescriptionService;
import com.dq.health.service.IAPIUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * <>
 *
 * @Author: chenxin
 * @Date: 2021/5/17
 */
@Service
public class PrescriptionServiceImpl implements IAPIPrescriptionService {
    private static final Logger LOGGER = LoggerFactory.getLogger(PrescriptionServiceImpl.class);

    @Resource
    PrescriptionOrderMapper prescriptionOrderMapper;

    @Autowired
    private IAPIGeneratorIdService generatorIdService;

    @Autowired
    private IAPIUserService userService;

    @Override
    public Long addPrescriptionOrder(SavePrescriptionRequest request) {
        Integer prescriptionType = request.getPrescriptionType();
        Long sickUserId = request.getSickUserId();
        Long operationUserId = request.getOperationUserId();
        Assert.notNull(prescriptionType, "处方单类型不能为空！");
        Assert.notNull(sickUserId, "患者id不能为空！");
        Assert.notNull(operationUserId, "操作人不能为空！");

        PrescriptionOrder prescriptionOrder = new PrescriptionOrder();
        String orderSn = generatorIdService.genId(GeneratorIdTypeEnum.PO);
        prescriptionOrder.setPrescriptionSn(orderSn);
        prescriptionOrder.setPrescriptionType(prescriptionType);

        UserBaseInfo userBaseInfo = userService.queryUserById(sickUserId);
        if (userBaseInfo == null) {
            LOGGER.error("患者不存在！sickUserId:{}", sickUserId);
            throw new BizRtException("患者不存在");
        }
        prescriptionOrder.setSickUserId(sickUserId);
        prescriptionOrder.setStatus(PrescriptionStatusEnum.WAIT.value());
        prescriptionOrder.setCreateUserId(operationUserId);
        prescriptionOrderMapper.insert(prescriptionOrder);

        return prescriptionOrder.getPrescriptionId();
    }

    @Override
    public void updatePrescriptionOrder(SavePrescriptionRequest request) {
        Long prescriptionOrderId = request.getPrescriptionOrderId();
        Long operationUserId = request.getOperationUserId();
        Assert.notNull(prescriptionOrderId, "处方单id不能为空！");
        Assert.notNull(operationUserId, "操作人不能为空！");

        PrescriptionOrder update = new PrescriptionOrder();
        BeanUtils.copyProperties(request, update);
        update.setUpdateUserId(operationUserId);
        prescriptionOrderMapper.updateById(update);
    }

    @Override
    public void dealWithPrescriptionOrder(SavePrescriptionRequest request) {
        Long prescriptionOrderId = request.getPrescriptionOrderId();
        Long doctorUserId = request.getDoctorUserId();
        Long operationUserId = request.getOperationUserId();
        Assert.notNull(prescriptionOrderId, "处方单id不能为空！");
        Assert.notNull(operationUserId, "操作人不能为空！");

        PrescriptionOrder update = new PrescriptionOrder();

        update.setUpdateUserId(operationUserId);
        prescriptionOrderMapper.updateById(update);

    }

    @Override
    public IPage<PrescriptionListInfo> queryPrescriptionList(QueryPrescriptionParam param) {
        return null;
    }

    @Override
    public PrescriptionDetailInfo getPrescriptionDetail(Long id) {
        return null;
    }

    @Override
    public void payOrder(Long id, Long operationUserId) {
        PrescriptionOrder prescriptionOrder = prescriptionOrderMapper.selectById(id);
        if (prescriptionOrder == null || prescriptionOrder.getStatus() == PrescriptionStatusEnum.DELETE.value()) {
            LOGGER.error("处方单不存在！");
            throw new BizRtException("处方单不存在");
        }

        UserBaseInfo userBaseInfo = userService.queryUserById(operationUserId);
        if (userBaseInfo == null) {
            LOGGER.error("支付操作人不存在！operationUserId:{}", operationUserId);
            throw new BizRtException("支付操作人不存在");
        }

        // 修改支付状态
        PrescriptionOrder update = new PrescriptionOrder();
        update.setPrescriptionId(id);
        update.setUpdateUserId(operationUserId);
        update.setPaymentStatus(1);
        prescriptionOrderMapper.updateById(update);

    }
}
